<template>
  <div class="room-content home page-tab">
    <Navbar :appTitle="title" :rtitle="rtitle" :rname="rname" />
    <div class="set-top">
      <div>
        <h1 class="title">
          常用 ({{ selectedlist.length }})
          <span class="save-btn" @click="saving">保存</span>
        </h1>
        <div class="menus-img">
          <template v-for="(u, i) in selectedlist">
            <!-- 申请表单菜单图标（本地图片） -->
            <img
              :key="i"
              :src="`${apiBaseUrlDownload}jksaas-mgt/mgt${u.attachment}`"
              v-if="u.attachment"
            />
            <img :key="i" src="../../assets/home/img/2.png" alt="" v-else />
          </template>
          <span class="emptyStyle" v-show="selectedlist.length > 12">...</span>
        </div>
      </div>
    </div>
    <div
      style="
        width: 100%;
        background: #fff;
        border-bottom: solid 2px #f0f0f0;
      "
    >
      <van-tabs style="width: 100%" v-model="selectIndex">
        <van-tab
          :title="`${item.name}（${item.selectTotal}/${item.total}）`"
          v-for="item in applyList"
          :key="item.id"
        ></van-tab>
      </van-tabs>
    </div>
    <div class="content-application">
      <!-- 发起菜单--begin -->
      <div class="menus-card" v-if="applyList[selectIndex]">
        <div
          v-for="item in applyList[selectIndex].mgtApplicationVoList"
          :key="item.id"
          class="menus-card-box"
        >
          <h1 class="title">{{ item.name }}</h1>
          <div style="width: 100%;">
            <div
              v-for="(res, j) in item.mgtApplicationVoList"
              :key="j"
              class="box menu flex-column align"
            >
              <div class="menu-img-box">
                <img
                  :src="`${apiBaseUrlDownload}jksaas-mgt/mgt${res.attachment}`"
                  class="imgicon"
                  alt=""
                  v-if="res.attachment"
                />
                <img
                  src="../../assets/home/img/2.png"
                  class="imgicon"
                  alt=""
                  v-else
                />
                <img
                  src="~@/assets/img/menu_del.png"
                  alt=""
                  class="menu-action"
                  @click="delFromMenus(res)"
                  v-if="res.isSelect"
                />
                <img
                  src="~@/assets/img/menu_add.png"
                  alt=""
                  class="menu-action"
                  @click="addToMenus(res)"
                  v-else
                />
              </div>
              <span class="label">{{ res.name }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Navbar from "@/components/navBar/navBar_right";
import { apiBaseUrlDownload } from "@/utils/request";
import { getAction, putAction } from "@/api/manage";
import { LING_YANG_ROLE_TYPE } from "@/store/mutation-types";
export default {
  name: "apply",
  components: { Navbar },
  data() {
    return {
      title: "常用设置",
      rtitle: "",
      rname: "", //search
      datalist: [],
      selectedlist: [], // 已选菜单
      apiBaseUrlDownload,
      debounce: false,

      applyList: [],
      selectTotal: 0,
      selectIndex: 0,
    };
  },
  computed: {},
  mounted() {},
  created() {
    this.getSelectedList();
  },
  methods: {
    getSelectedList() {
      let antelopeFlag = this.$ls.get(LING_YANG_ROLE_TYPE);
      let params = { useScope: "app" };
      if (antelopeFlag) {
        params.antelopeFlag = antelopeFlag;
      }
      getAction("/sys/home/queryMyself", params).then((res) => {
        this.selectedlist = res.result || [];
        for (let i = 0; i < this.selectedlist.length; i++) {
          const item = this.selectedlist[i];
          item.id = item.applicationId;
        }
      });
      getAction("/sys/home/application", params).then((res) => {
        if (res.code == 200) {
          this.applyList = res.result;
          if (this.applyList.length > 0) {
            this.selectTotal = 0;
            this.selectIndex = 0;
            this.applyList.forEach((item) => {
              this.selectTotal += item.selectTotal;
            });
          }
        }
      });
    },
    delFromMenus(app) {
      console.log(app, this.selectedlist);
      for (let i = 0; i < this.selectedlist.length; i++) {
        const item = this.selectedlist[i];
        if (item.applicationId == app.id || item.id == app.id) {
          this.$set(app, "isSelect", null);
          this.selectedlist.splice(i, 1);
          this.applyList[this.selectIndex].selectTotal -= 1;
          return;
        }
      }
    },
    addToMenus(app) {
      this.$set(app, "isSelect", "1");
      this.applyList[this.selectIndex].selectTotal += 1;
      this.selectedlist.push(app);
    },
    saving() {
      if (this.debounce) {
        return;
      } else {
        this.debounce = true;

        let arr = [];
        this.selectedlist.forEach((item, i) => {
          item.orderNum = i;
          item.applicationId = item.id;
          let obj = {
            applicationId: item.id,
            attachment: item.attachment,
            name: item.name,
            orderNum: i,
            path: item.path,
            useScope: item.useScope,
          };
          arr.push(obj);
        });
        putAction("/sys/home/update", arr).then((res) => {
          if (res.code == 200) {
            this.debounce = false;
            this.$toast("保存成功!");
            this.$router.go(-1);
          } else {
            this.$toast(res.message);
            this.debounce = false;
          }
        });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
$placeholder: #ccc;
$fontColortit: #191f25;
$fonttit: 0.15rem;
$fonttxt: 0.14rem;

::-webkit-input-placeholder {
  color: $placeholder;
}
::-moz-placeholder {
  color: $placeholder;
}

:-ms-input-placeholder {
  color: $placeholder;
}
/*公共class部分*/

/*通用标题*/
.publicTit {
  font-size: $fontColortit;
  color: red;
}
::-webkit-input-placeholder {
  color: $placeholder;
}

::-moz-placeholder {
  color: $placeholder;
}

:-ms-input-placeholder {
  color: $placeholder;
}

.home {
  background: #f4f5f6;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .bg {
    height: 100%;
    background: #f4f5f6;
    .radius {
      height: 340px;
      background: #1e8df5;
      border-radius: 0px 0px 20px 20px;
    }
  }
  .box {
    width: 25%;
    display: inline-flex;
    margin-bottom: 30px;
  }
  .set-top {
    background: #fff;
    padding: 20px 32px;
    margin-bottom: 20px;
    .menus-img {
      width: 100%;
      // height: 0.21rem;
      height: 48px;
      overflow: hidden;
      position: relative;
      img {
        width: 46px;
        height: 46px;
        margin-right: 36px;
      }
    }
    .title {
      font-size: 32px;
      color: $fontColortit;
      font-weight: 500;
      display: flex;
      justify-content: space-between;
      .save-btn {
        color: #1890ff;
        cursor: pointer;
      }
    }
  }
  .content-application {
    flex: 1;
    overflow-y: auto;
    .project-name {
      font-size: 36px;
      color: rgb(0, 0, 0);
      font-weight: normal;
    }
    .info-list {
      margin: 50px 0;
      .info {
        flex: 1;
        .info-label {
          font-size: 24px;
          color: #f4f5f6;
        }
        .info-value {
          font-size: 36px;
          color: #fff;
        }
      }
    }
    .menus-card-box {
      background: #ffffff;
      margin-bottom: 10px;
    }
    .menus-card {
      flex-flow: wrap;
      padding-bottom: 0;
      border-radius: 5px;
      .title {
        color: $fontColortit;
        font-size: 30px;
        width: 100%;
        padding: 10px 16px;
      }
      .icon-img {
        font-size: 22px !important;
        color: #fff;
      }
      .menu {
        width: 25%;
        margin-bottom: 30px;
        .menu-img-box {
          position: relative;
          .menu-action {
            width: 24px;
            height: 24px;
            position: absolute;
            right: -12px;
            top: -12px;
          }
        }
        .icon {
          display: flex;
          align-items: center;
          justify-content: center;
          border: 1px solid rgb(0, 106, 255);
          background: rgb(0, 106, 255);
          border-radius: 10px;
          width: 80px;
          height: 80px;
        }
        .label {
          display: inline-block;
          // height: 40px;
          text-align: center;
          width: 100%;
          line-height: 20px;
          // overflow: hidden;
          // text-overflow: ellipsis;
          // white-space: nowrap;
          margin-top: 20px;
          padding: 0;
          font-size: 26px !important;
          color: $fontColortit;
        }
      }
      .app-menus {
        padding: 32px 0;
        border-bottom: 2px solid #f0f0f0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .menu-action {
          width: 30px;
          height: 30px;
        }
        .imgicon {
          margin-right: 24px;
        }
      }
    }
    .tab-list {
      padding: 22px 0;
      .tab {
        margin-right: 88px;
        &:last-child {
          margin-right: 0;
        }
      }
      .label {
        line-height: 48px;
        font-size: $fonttit;
        color: $fontColortit;
      }
      .border {
        display: block;
        opacity: 0;
        margin-top: 4px;
        width: 28px;
        height: 2px;
        background: #1e8df5;
        border-radius: 1px;
      }
      .selected .border {
        opacity: 1;
      }
      .van-badge {
        right: -10px;
      }
    }
  }
  .list {
    .item {
      padding: 18px 24px;
      background: #fff;
      border-radius: 5px;
      font-size: 24px;
      .time {
        color: #929292;
      }
      .title {
        margin: 26px 0;
        font-size: 28px;
        color: $fontColortit;
      }
      .user-info {
        padding-bottom: 20px;
        font-size: 24px;
        color: #323232;
        border-bottom: 1px solid #eeeeee;
        .icon {
          width: 24px;
        }
        .label {
          margin: 0 20px;
        }
      }
      .bottom {
        padding-top: 24px;
        font-size: 24px;
        color: #888;
        button {
          height: 24px;
          padding: 0 20px;
          border-radius: 22px;
          border: 1px solid #dfdfdf;
        }
      }
    }
  }
}
.imgicon {
  width: 60px !important;
  height: 60px !important;
}
/deep/ .van-tabs__line {
  background-color: #1890ff;
}
/deep/ .van-tab--active {
  color: #1890ff;
}
.emptyStyle {
  position: absolute;
  right: 0;
  top: 0;
  font-size: 32px;
}
</style>
